<?php

namespace App\Http\Controllers\Backstage\Link;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
//友链管理
class LinkController extends Controller
{
    //获取友链列表
    public function getLink(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('link')
            ->leftjoin('upload_file', 'link.fileid', '=', 'upload_file.fileid')
            ->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['link_create_time'] = date('Y-m-d', $value['link_create_time']);
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $value['url'] . "/" . $value['file_name'];
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //获取友链详情
    public function getLinkdetail(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['link_id'] = (int) $request->query('link_id');
        $info = DB::table('link')
            ->where($map)
            ->leftjoin('upload_file', 'link.fileid', '=', 'upload_file.fileid')
            ->first();
        if ($info) {
            $info->file_name = http_type() . $_SERVER['HTTP_HOST'] . '/' . $info->url . '/'  . $info->file_name;
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //新增友链
    public function addLink(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['link_title'] = addslashes($request->input('link_title'));
        $date['link_introduce'] = addslashes($request->input('link_introduce'));
        $date['link_address'] = addslashes($request->input('link_address'));
        $date['link_status'] = (int) $request->input('link_status');
        $date['link_appid']=addslashes($request->input("link_appid"));
        $date['fileid'] = (int) $request->input('fileid');
        $date['hits'] = 0;
        $date['link_create_time'] = time();
        $msg = DB::table('link')->insert($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //修改友链
    public function editLink(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['link_id'] = (int) $request->input('link_id');
        $date['link_title'] = addslashes($request->input('link_title'));
        $date['link_introduce'] = addslashes($request->input('link_introduce'));
        $date['link_address'] = addslashes($request->input('link_address'));
        $date['link_status'] = (int) $request->input('link_status');
        $date['link_appid']=addslashes($request->input("link_appid"));
        $date['fileid'] = (int) $request->input('fileid');
        $msg = DB::table('link')->where($map)->update($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //删除友链
    public function delLink(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['link_id'] = (int) $request->input('link_id');
        $msg = DB::table('link')->where($map)->delete();
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
